<?php

namespace App\Crontab;

use App\Model\Mysql\OnlineOrderModel;
use App\Service\OrderService;
use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
use EasySwoole\EasySwoole\Task\TaskManager;

class OrderCancel extends AbstractCronTask
{

    public static function getRule(): string
    {
        return '*/1 * * * *';
    }

    public static function getTaskName(): string
    {
        return 'orderCancel';
    }

    /**
     * 订单取消
     * @param int $taskId
     * @param int $workerIndex
     * @throws \Throwable
     */
    function run(int $taskId, int $workerIndex)
    {
        $order = OnlineOrderModel::getList(['order_status' => 1], ['id', 'create_time']);
        // 超过15 分钟取消订单
        foreach ($order as $value) {
            if ((time() - $value['create_time']) > 900) {
                OnlineOrderModel::editData(['order_status' => 2], ['id' => $value['id']]);
                OrderService::orderCancelOperate($value['id']);
            }
        }
//        // 开发者可投递给task异步处理
//        TaskManager::getInstance()->async(function (){
//            // todo some thing
//        });
    }

    function onException(\Throwable $throwable, int $taskId, int $workerIndex)
    {
        echo $throwable->getMessage();
    }
}
